package com.ruicar.afs.cloud.channel.cardealer.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruicar.afs.cloud.channel.cardealer.common.Constants;
import com.ruicar.afs.cloud.channel.cardealer.condition.CarDealerCondition;
import com.ruicar.afs.cloud.channel.cardealer.condition.DirectCarDealerCondition;
import com.ruicar.afs.cloud.channel.cardealer.entity.CommonCarDealer;
import com.ruicar.afs.cloud.channel.cardealer.mapper.CarDealerMapper;
import com.ruicar.afs.cloud.channel.cardealer.service.CarDealerService;
import com.ruicar.afs.cloud.channel.cardealer.vo.CarDealerVo;
import com.ruicar.afs.cloud.channel.cardealer.vo.ChannelInfoVo;
import com.ruicar.afs.cloud.channel.cardealer.vo.CommonCarDealerVo;
import com.ruicar.afs.cloud.channel.cardealer.vo.DirectCarDealerVo;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: sang jian
 * @date: 2020/5/14 10:55
 * @description:
 */
@Service
public class CarDealerServiceImpl extends ServiceImpl<CarDealerMapper, CommonCarDealer> implements CarDealerService {


    @Override
    public IPage<CommonCarDealerVo> getCommonCarDealerList(Page page, CarDealerCondition carDealerCondition) {
        if (carDealerCondition.getChannelId() != null) {
            return baseMapper.getCommonCarDealerListForApply(page, carDealerCondition);
        }
        return baseMapper.getCommonCarDealerList(page, carDealerCondition);
    }

    @Override
    /**
     根据车商id获取渠道信息
     */
    public List<ChannelInfoVo> getChannelsByCardealerId(String id) {
        return baseMapper.getChannelsByCardealerId(id);
    }


    @Override
    public IResponse diableCarDealer(String[] channelIds, String[] ids) {
        for (int i = 0; i < ids.length; i++) {
            int result = baseMapper.diableCarDealer(channelIds[i], ids[i]);
            if (result == 0) {
                return IResponse.fail("第" + (i + 1) + "个车商禁用失败");
            }
        }
        return IResponse.success("禁用成功");
    }


    @Override
    /**根据合作商账户返回此账户下的车商列表信息*/
    public IResponse<CarDealerVo> getCarDealersByUserId(String userId, String cardealerType) {
        if (Constants.COMMONCARDEALER.equals(cardealerType)) {
            return IResponse.success(baseMapper.getCommonCarDealersByUserId(userId, cardealerType));
        } else if (Constants.DIRECTCARDEALER.equals(cardealerType)) {
            return IResponse.success(baseMapper.getDirectCarDealersByUserId(userId, cardealerType));
        }
        return IResponse.fail("查询失败").setData(null);
    }


    @Override
    public IPage<DirectCarDealerVo> getDirectCarDealerList(Page page, CarDealerCondition carDealerCondition) {

        if (carDealerCondition.getChannelId() != null) {
            return baseMapper.getDirectCarDealerListForApply(page, carDealerCondition);
        }
        return baseMapper.getDirectCarDealerList(page, carDealerCondition);
    }



    /**
     * 获取直营车商展业区域
     */
    @Override
    public IPage<DirectCarDealerVo> getAreaListCondition(Page page, DirectCarDealerCondition condition) {
        return baseMapper.getAreaListInfo(page, condition);
    }


}
